package com.jinit.jia.boot;

import com.jinit.jia.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.DeferredImportSelector;
import org.springframework.core.io.support.SpringFactoriesLoader;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.util.StringUtils;

import java.util.*;

/**
 * AutoConfigurationImportSelector
 *
 * @author JInit
 * @date 2021-08-05
 */
public class AutoConfigurationImportSelector implements DeferredImportSelector {

    private final List<AutoConfigurationEntry> autoConfigurationEntries = new ArrayList<>();

    @Override
    public String[] selectImports(AnnotationMetadata annotationMetadata) {
        List<String> configurations = SpringFactoriesLoader.loadFactoryNames(EnableAutoConfiguration.class,
                this.getClass().getClassLoader());
        return configurations.toArray(new String[configurations.size()]);
    }

    private static class AutoConfigurationGroup implements DeferredImportSelector.Group {

        @Override
        public void process(AnnotationMetadata annotationMetadata, DeferredImportSelector deferredImportSelector) {
        }

        @Override
        public Iterable<Entry> selectImports() {
            return null;
        }
    }


    protected static class AutoConfigurationEntry {

        private final List<String> configurations;

        private final Set<String> exclusions;

        private AutoConfigurationEntry() {
            this.configurations = Collections.emptyList();
            this.exclusions = Collections.emptySet();
        }

        AutoConfigurationEntry(Collection<String> configurations, Collection<String> exclusions) {
            this.configurations = new ArrayList<>(configurations);
            this.exclusions = new HashSet<>(exclusions);
        }

        public List<String> getConfigurations() {
            return this.configurations;
        }

        public Set<String> getExclusions() {
            return this.exclusions;
        }

    }
}
